﻿@model MaterialProcess
@{
    ViewBag.Title = "加工单维护";
    Layout = "~/Views/Shared/_LayoutFormDetail.cshtml";
}
@section Up{
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.OrderState)
    @Html.nbDateFor(model => model.ProcessDate).Required()
    @Html.nbTextBoxFor(model => model.TotalWeight).ReadOnly()
}
@section Down{
    @(Html.nbDetailFor<MaterialProcessItem>(s =>
    {
        s.ID("MaterialList");
        s.layoutW(10);
        s.layoutH(130);
        s.Columns(col =>
        {
            col.Bind(e => e.Id).IsKey();
            col.Bind(e => e.MaterialId).Hide();
            col.Bind(e => e.Date).Hide();
            col.Bind(e => e.Code).Width(30);
            col.Bind(e => e.Name).Width(60).Align(GridAlign.Left);
            col.BindEnum(e => e.BigClass, "BigClass").Width(30);
            col.BindEnum(e => e.Category, "MaterialCategory").Width(50);
            col.Bind(e => e.RawMaterialId).Hide();
            col.Bind(e => e.RawMaterialName).Width(60).Align(GridAlign.Left);
            col.Bind(e => e.ProcessRate).Width(40).Align(GridAlign.Right);
            col.BindControl(e => e.ProcessWeight).Align(GridAlign.Right).Width(50);
            col.Bind(e => e.PickWeight).Align(GridAlign.Right).Width(50);
            col.Bind(e => e.StorageWeight).Align(GridAlign.Right).Width(50);
            col.BindTemplate(50)
                .Template(@<a href="javascript:DeleteRow('${Id}')" class="imgbtn2"><span class="Delete">删除</span></a>);
        });
        s.Buttons(b =>
        {
            b.Bind("Add", "选择品名", GridButtonType.Add).OnClick("SelectMaterial");
        });
    }))
}
<script type="text/javascript">
    function SelectMaterial(e) {
        OpenModalDialog('@Url.Action("LookUp", "Material", new { NeedProcess = true, Type = ViewBag.Type })',
        {
            width: 800,
            height: 500,
            type: 2,
            onclose:
            function (MaterialData) {
                if (MaterialData) {
                    MapJsonArray(MaterialData, "Id", "MaterialId");
                    $("#MaterialList").flexAddRows(MaterialData);
                    AutoSum();
                }
            }
        });
    }

    function DeleteRow(id) {
        $("#MaterialList").flexDeleteRowData(id);
        AutoSum();
    }

    $(function () {
        $("#MaterialList").delegate("#MaterialList input[name='ProcessWeight']", 'change', function (e) {
            AutoSum();
        })
    });

    function AutoSum() {
        var data = $("#MaterialList").flexGetData();
        var TotalCost = 0;
        for (var i = 0; i < data.length; i++) {
            var Cost = data[i].ProcessWeight;
            TotalCost += Cost * 1;
        }
        $('#TotalWeight').val(parseFloat(TotalCost).toFixed(2));
    }
</script>
